/**
 * Created by KhoaNam on 7/12/2014.
 */

var MyLayer = cc.Layer.extend({
    ctor: function(){
        this._super();
        this.childHasEvents = [];
        this.canCatchEvent = true;
    },
    init: function () {
        this._super();
        //this.setTouchMode(cc.TOUCH_);
        this.setTouchEnabled(true);
        this.setMouseEnabled(true);
    },

    addChildHasEvent: function(child, order){
        console.log("add");
        for(var i = 0 ; i < this.childHasEvents.length ; i++){
            if(child == this.childHasEvents[i]){
                return;
            }
        }
        if(order){
            this.addChild(child, order);
        }
        else{
            this.addChild(child);
        }

        this.childHasEvents.push(child);
    },

    removeChildHasEvent: function(child){
        this.removeChild(child);
        for(var i = 0 ; i < this.childHasEvents.length ; i++){
            if(this.childHasEvents[i] == child){
                this.childHasEvents.splice(i, 1);
                break;
            }
        }
    },

    removeAllChildHasEvent: function(){
        while(this.childHasEvents.length > 0){
            var child = this.childHasEvents.pop();
            this.removeChild(child);
        }
    },

    //Override remove all
    removeAllChildren: function(){
        this._super();
        while(this.childHasEvents.length > 0){
            this.childHasEvents.pop();
        }
    },

    onTouchesBegan: function(touches, event){
        if(this.canCatchEvent){
            for(var i = 0 ; i < this.childHasEvents.length ; i++){
                if(this.childHasEvents[i].enableEvent &&
                    cc.rectContainsPoint(this.childHasEvents[i].getBoundingBox(),touches[0].getLocation()) == true){
                    this.childHasEvents[i].onTouchBegan(touches[0], event);
                }
            }
        }
    },

    onTouchesEnded:function (touches, event) {
        if(this.canCatchEvent){
            for(var i = 0 ; i < this.childHasEvents.length ; i++){
                if(this.childHasEvents[i].enableEvent &&
                    cc.rectContainsPoint(this.childHasEvents[i].getBoundingBox(),touches[0].getLocation()) == true){
                    this.childHasEvents[i].onTouchEnded(touches[0], event);
                }
            }
        }
    },

    onTouchesEnter: function(touches, event){
        console.log("enter");
    },

    onMouseMoved: function(mouse, event){
        if(this.canCatchEvent){
            for(var i = 0 ; i < this.childHasEvents.length ; i++){
                if(this.childHasEvents[i].enableEvent){
                    if(cc.rectContainsPoint(this.childHasEvents[i].getBoundingBox(), mouse.getLocation()) == true){
                        if(!this.childHasEvents[i].isMouseOver){
                            this.childHasEvents[i].isMouseOver = true;
                            this.childHasEvents[i].onMouseOver(mouse, event);
                        }
                    }
                    else{
                        if(this.childHasEvents[i].isMouseOver){
                            this.childHasEvents[i].isMouseOver = false;
                            this.childHasEvents[i].onMouseOut(mouse, event);
                        }
                    }
                }
            }
        }
    }


});
